package rc;

import java.io.FileInputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import rc.db.BankDBRCAware;
import rc.db.BankDBRCNotAware;
import rc.memory.BankMemoryRCAware;
import rc.memory.BankMemoryRCNotAware;

public class BankMain
{
	public static void main(String[] args) throws Exception
	{
		Employee e1, e2, e3;
		Bank bank;

		Properties prop = new Properties();
		FileInputStream fis = new FileInputStream("config.xml");
		prop.loadFromXML(fis);
		boolean vulnerable = Boolean.parseBoolean(prop
				.getProperty("vulnerable"));
		String driver = prop.getProperty("driver");
		List<Integer> balances = Arrays.asList(30, 50, 100);

		if (!driver.equals("database") && !driver.equals("memory")) {
			throw new RuntimeException(
					"Driver must be \"database\" or \"memory\"");
		}

		if (vulnerable) {
			if (driver.equals("database")) {
				bank = new BankDBRCNotAware(balances,
						prop.getProperty("database.url"),
						prop.getProperty("database.user"),
						prop.getProperty("database.password"));
			} else {
				bank = new BankMemoryRCNotAware(balances);
			}
		} else {
			if (driver.equals("database")) {
				bank = new BankDBRCAware(balances,
						prop.getProperty("database.url"),
						prop.getProperty("database.user"),
						prop.getProperty("database.password"));
			} else {
				bank = new BankMemoryRCAware(balances);
			}
		}

		// transfers build a circle
		e1 = new Employee(bank, 0, 1, 20);
		e2 = new Employee(bank, 1, 2, 30);
		e3 = new Employee(bank, 2, 0, 40);

		System.out.println("Before:");
		bank.showBalances();

		e1.start();
		e2.start();
		e3.start();
	}
}